﻿using Bilibili.Common.Log;
using Bilibili.Model.Common;
using Bilibili.Model.Resource;
using System;
using System.Collections.Generic;
using System.Net;

namespace Bilibili.Common.Api
{
    public abstract class ApiClientBase
    {
        protected abstract IApiClient ApiClient { get; }
        protected abstract string GetSignUrl(string original, ApiKeyInfo apiKeyInfo = null);
        protected ReturnJObject BuildJExceptionResult(Exception exception)
        {
            LogManager.Instance.LogError("BuildJExceptionResult", exception);
            return new ReturnJObject
            {
                Code = -1,
                Message = exception.Message
            };
        }
        protected ReturnModel BuildExceptionResult(Exception exception)
        {
            LogManager.Instance.LogError("BuildExceptionResult", exception);
            return new ReturnModel
            {
                Success = false,
                Message = exception.Message
            };
        }
        protected ApiDataModel<T> BuildApiFailedResult<T>(string message = "")
        {
            return new ApiDataModel<T>
            {
                Code = -1,
                Message = message
            };
        }
        protected ApiDataModel<T> BuildApiExceptionResult<T>(Exception exception)
        {
            LogManager.Instance.LogError("BuildApiExceptionResult", exception);
            return new ApiDataModel<T>
            {
                Code = -1,
                Message = exception.Message
            };
        }
        protected ReturnModel<T> BuildExceptionResult<T>(Exception exception)
        {
            LogManager.Instance.LogError("BuildExceptionResult", exception);
            return new ReturnModel<T>
            {
                Success = false,
                Message = exception.Message
            };
        }
        protected ReturnModel BuildFailedResult(string msg = "")
        {
            return new ReturnModel
            {
                Success = false,
                Message = msg
            };
        }
        protected ReturnPlayModel BuildPlayFailedResult(string msg = "")
        {
            return new ReturnPlayModel
            {
                Success = false,
                Message = msg
            };
        }
        protected ReturnModel<T> BuildFailedResult<T>(string msg = "")
        {
            return new ReturnModel<T>
            {
                Success = false,
                Message = msg
            };
        }
        protected ReturnModel BuildSuccessResult(string msg = "")
        {
            return new ReturnModel
            {
                Success = true,
                Message = msg
            };
        }
        protected ReturnPlayModel BuildPlaySuccessResult(IEnumerable<string> urls, int qn, DataSourceHeader header)
        {
            return new ReturnPlayModel
            {
                Success = true,
                Urls = urls,
                Headers = header
            };
        }
        protected ReturnPlayModel BuildPlaySuccessResult(IEnumerable<string> urls, int qn, DataSourceHeader header, dynamic sources)
        {
            return new ReturnPlayModel
            {
                Success = true,
                Urls = urls,
                Headers = header,
                MediaSources = sources,
                SourceMode = SourceMode.Dash
            };
        }
        protected ReturnPlayModel BuildPlaySuccessResult(IEnumerable<string> urls, int qn, string referer, string userAgent = "")
        {
            return new ReturnPlayModel
            {
                Success = true,
                Urls = urls,
                Headers = new DataSourceHeader(referer, userAgent)
            };
        }
        protected ReturnModel<T> BuildSuccessResult<T>(T data, string msg = "")
        {
            return new ReturnModel<T>
            {
                Success = true,
                Data = data,
                Message = msg
            };
        }
        protected ReturnModel<T> BuildSuccessResult<T>(T data, CookieCollection cookieCollection, string msg = "")
        {
            return new ReturnModel<T>
            {
                Success = true,
                Cookies = cookieCollection,
                Data = data,
                Message = msg
            };
        }
    }
}